package seotools.positions;

import obj.Accum;

import java.io.*;
import java.util.Properties;

public class Extramobi {
    public static final String DELIMETER = ";";
    public static final String NA = "Не известно";

    public static Properties props = new Properties();

    public static int counter = 0;

    public static void main(String[] args) throws IOException {
        props.load(new FileReader("goods.properties"));
        PrintStream out = new PrintStream(new FileOutputStream("goods-price.txt"));
        out.printf(String.valueOf(props.get("header")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        out.printf(String.valueOf(props.get("accum")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        fillAccumsByCategory(out, "accum.nokia", 1.25);
        fillAccumsByModel(out, "accum.nokia", "Nokia", 1.25);
        fillAccumsByCategory(out, "accum.nokiamaxcell", 1);
        fillAccumsByCategory(out, "accum.samsung", 1);
        fillAccumsByCategory(out, "accum.se", 1);
        fillAccumsByCategory(out, "accum.lg", 1);
        out.printf(String.valueOf(props.get("holder")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        out.printf(String.valueOf(props.get("charger")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        out.printf(String.valueOf(props.get("charger.network")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        fillChargersByCategory(out, "Nokia", 1, false, 5);
        out.printf(String.valueOf(props.get("charger.auto")), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        fillChargersByCategory(out, "Nokia", 1, true, 5);
    }

    private static void fillAccumsByModel(PrintStream out, String category, String devicePrefix, double coef) throws IOException {
        String prefix = devicePrefix;
        String fileName = category + ".txt";
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = reader.readLine()) != null) {
            Accum accum = Accum.parseAccum(line, coef);
            for (String device : accum.devices) {
                String deviceFullName = devicePrefix + device;
                devicesToCompatibleURL(accum.devices, deviceFullName);
                out.printf(String.valueOf(props.get(category + ".device.desc")), accum.name + DELIMETER,
                        device + DELIMETER,
                        device.replace(" ", "_") + DELIMETER,
                        accum.price + DELIMETER,
                        accum.oldPrice + DELIMETER,
                        accum.count + DELIMETER,
                        device, device, devicesToDescription(accum.devices, category), accum.name, accum.name, DELIMETER,
                        accum.name, accum.name, accum.name, accum.name, accum.name, accum.vendor, accum.model, accum.power, accum.type, accum.volt, accum.name, devicesToDescription(accum.devices, category), accum.name, accum.name, accum.name.replace(" ", "+"), accum.name, accum.name, DELIMETER,
                        accum.name, devicesToSmallDescription(accum.devices) + DELIMETER,
                        accum.name, devicesToSmallDescription(accum.devices) + DELIMETER,
                        counter++ + DELIMETER,
                        accum.name + devicesToTitle(accum.devices), DELIMETER,
                        accum.name + devicesToTitle(accum.devices), DELIMETER,
                        accum.name + devicesToTitle(accum.devices) + DELIMETER,
                        accum.name + devicesToTitle(accum.devices) + DELIMETER,
                        accum.name + devicesToTitle(accum.devices) + DELIMETER,
                        accum.name + devicesToTitle(accum.devices) + DELIMETER,
                        accum.name, accum.name, accum.name);
            }
            out.println();
        }
    }

    public static void fillAccumsByCategory(PrintStream out, String category, double coef) throws IOException {
        out.printf(String.valueOf(props.get(category)), DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER);
        out.println();
        String fileName = category + ".txt";
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = reader.readLine()) != null) {
            Accum accum = Accum.parseAccum(line, coef);
            out.printf(String.valueOf(props.get(category + ".desc")), accum.name + DELIMETER,
                    accum.name + DELIMETER,
                    accum.name.replace(" ", "_") + DELIMETER,
                    accum.price + DELIMETER,
                    accum.oldPrice + DELIMETER,
                    accum.count + DELIMETER,
                    accum.name, accum.name, devicesToDescription(accum.devices, category), accum.name, accum.name, DELIMETER,
                    accum.name, accum.name, accum.name, accum.name, accum.name, accum.vendor, accum.model, accum.power, accum.type, accum.volt, accum.name, devicesToDescription(accum.devices, category), accum.name, accum.name, accum.name.replace(" ", "+"), accum.name, accum.name, DELIMETER,
                    accum.name, devicesToSmallDescription(accum.devices) + DELIMETER,
                    accum.name, devicesToSmallDescription(accum.devices) + DELIMETER,
                    counter++ + DELIMETER,
                    accum.name + devicesToTitle(accum.devices), DELIMETER,
                    accum.name + devicesToTitle(accum.devices), DELIMETER,
                    accum.name + devicesToTitle(accum.devices) + DELIMETER,
                    accum.name + devicesToTitle(accum.devices) + DELIMETER,
                    accum.name + devicesToTitle(accum.devices) + DELIMETER,
                    accum.name + devicesToTitle(accum.devices) + DELIMETER,
                    accum.name, accum.name, accum.name);
            out.println();
        }

    }

    public static void fillChargersByCategory(PrintStream out, String name, double coef, boolean isAuto, int columns) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("charger." + name.toLowerCase() + ".txt"));
        String fullCategory;
        String imageName;
        String[] line = reader.readLine().split(";");
        int price;
        if (isAuto) {
            fullCategory = "charger.auto." + name.toLowerCase();
            imageName = "charger-auto-" + name.toLowerCase();
            price = Integer.valueOf(line[2]);
        } else {
            fullCategory = "charger.network." + name.toLowerCase();
            imageName = "charger-network-" + name.toLowerCase();
            price = Integer.valueOf(line[1]);
        }
        String[] models = line[0].split(",");
        int count = Integer.valueOf(line[3]);
        int oldPrice = (int) (price * coef);
        String modelsStr = modelsToTable(models, name, columns);
        out.printf(String.valueOf(props.get(fullCategory)), DELIMETER, DELIMETER, DELIMETER, price + DELIMETER, oldPrice + DELIMETER, count + DELIMETER, DELIMETER, modelsStr, DELIMETER, DELIMETER, DELIMETER, counter++ + DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, DELIMETER, imageName, imageName, imageName);
        out.println();
    }

    private static String modelsToTable(String[] models, String name, int columns) {
        StringBuilder modelsSB = new StringBuilder("<table style=\"font-size:11px\">");
        for (int i = 1; i < models.length; i++) {
            if (i == 1 && i % columns + 1 == 0) {
                modelsSB.append("<tr>");
            }
            modelsSB.append("<td><strong>").append(name).append(" ").append(models[i - 1]).append("</strong></td>");
            if (i % columns == 0) {
                modelsSB.append("</tr>");
            }
        }
        if (modelsSB.toString().endsWith("</td>")) {
            modelsSB.append("</tr>");
        }
        modelsSB.append("</table>");
        return modelsSB.toString();
    }

    public static String devicesToDescription(String[] devices, String category) {
        StringBuilder sb = new StringBuilder("<ul>");
        for (String device : devices) {
            String item = String.format(String.valueOf(props.get(category + ".item")), device);
            sb.append(item);
        }
        sb.append("</ul><br/>");
        return sb.toString();
    }

    public static String devicesToCompatibleURL(String[] devices, String currDevice) {
        StringBuilder sb = new StringBuilder("<ul>");
        for (String device : devices) {
            if (device.equals(currDevice)) {
                continue;
            }
            String item = "<a href=\"http://extramobi.ru/category/" + device.replace(" ", "_") + ">" + device + "";
            sb.append(item);
        }
        sb.append("</ul><br/>");
        return sb.toString();
    }

    public static String devicesToSmallDescription(String[] devices) {
        StringBuilder sb = new StringBuilder();
        for (String device : devices) {
            sb.append(",<strong>").append(device).append("</strong>");
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }

    public static String devicesToURL(String[] devices) {
        StringBuilder sb = new StringBuilder();
        for (String device : devices) {
            sb.append("_").append(device.replace(" ", "_"));
        }
        return sb.toString();
    }

    public static String devicesToTitle(String[] devices) {
        StringBuilder sb = new StringBuilder();
        for (String device : devices) {
            sb.append(" ").append(device);
        }
        return sb.toString();
    }
}